package com.tedu.pj.common.aspect;

import com.tedu.pj.common.anntation.RequireCache;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/22
 */
public class CacheAdvice implements MethodInterceptor {
    Map<String,Object> map = new ConcurrentHashMap<String,Object>();


    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {

        Class<?> aClass1 = invocation.getThis().getClass();
        Method declaredMethod = aClass1.getDeclaredMethod(invocation.getMethod().getName(), invocation.getMethod().getParameterTypes());

        RequireCache annotation =declaredMethod.getAnnotation(RequireCache.class);
        if (annotation!=null){
            Object list =map.get(annotation.value());
            if (list!=null){
                return list;
            }
        }
        Object proceed = invocation.proceed();
        map.put(annotation.value(),proceed);
        return proceed;
    }
}
